home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / FLSBUF.C < prev    next >
C/C++ Source or Header  |  1992-03-02  |  3KB  |  156 lines

  1. /* This is file FLSBUF.C */
  2. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  3. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  4. ** Rochester NH, 03867-2954, USA.
  5. */
  6.  
  7. /*
  8.  * Copyright (c) 1980 Regents of the University of California.
  9.  * All rights reserved.  The Berkeley software License Agreement
  10.  * specifies the terms and conditions for redistribution.
  11.  */
  12.  
  13. #if defined(LIBC_SCCS) && !defined(lint)
  14. static char sccsid[] = "@(#)flsbuf.c    5.5 (Berkeley) 6/18/90";
  15. #endif LIBC_SCCS and not lint
  16.  
  17. #include    <stdio.h>
  18. #include    <sys/types.h>
  19. #include    <sys/stat.h>
  20.  
  21. char    *malloc();
  22.  
  23. _flsbuf(c, iop)
  24. unsigned c;
  25. register FILE *iop;
  26. {
  27.     register char *base;
  28.     register n, rn;
  29.     char c1;
  30.     int size;
  31.     struct stat stbuf;
  32.  
  33.     if (iop->_flag & _IORW) {
  34.         iop->_flag |= _IOWRT;
  35.         iop->_flag &= ~(_IOEOF|_IOREAD);
  36.     }
  37.  
  38.     if ((iop->_flag&_IOWRT)==0)
  39.         return(EOF);
  40. tryagain:
  41.     if (iop->_flag&_IOLBF) {
  42.         base = iop->_base;
  43.         *iop->_ptr++ = c;
  44.         if ((rn = iop->_ptr - base) >= iop->_bufsiz || c == '\n') {
  45.             iop->_ptr = base;
  46.             iop->_cnt = 0;
  47.         } else {
  48.             /* we got here because _cnt is wrong, so fix it */
  49.             iop->_cnt = -rn;
  50.             rn = n = 0;
  51.         }
  52.     } else if (iop->_flag&_IONBF) {
  53.         c1 = c;
  54.         rn = 1;
  55.         base = &c1;
  56.         iop->_cnt = 0;
  57.     } else {
  58.         if ((base=iop->_base)==NULL) {
  59.             if (fstat(fileno(iop), &stbuf) < 0 ||
  60.                 stbuf.st_blksize <= NULL)
  61.                 size = BUFSIZ;
  62.             else
  63.                 size = stbuf.st_blksize;
  64.             if ((iop->_base=base=malloc(size)) == NULL) {
  65.                 iop->_flag |= _IONBF;
  66.                 goto tryagain;
  67.             }
  68.             iop->_flag |= _IOMYBUF;
  69.             iop->_bufsiz = size;
  70.             if (iop==stdout && isatty(fileno(stdout))) {
  71.                 iop->_flag |= _IOLBF;
  72.                 iop->_ptr = base;
  73.                 goto tryagain;
  74.             }
  75.             rn = n = 0;
  76.         } else
  77.             rn = iop->_ptr - base;
  78.         iop->_ptr = base;
  79.         iop->_cnt = iop->_bufsiz;
  80.     }
  81.     while (rn > 0) {
  82.         n = write(fileno(iop), base, rn);
  83.         if (n <= 0) {
  84.             iop->_flag |= _IOERR;
  85.             return(EOF);
  86.         }
  87.         rn -= n;
  88.         base += n;
  89.     }
  90.     if ((iop->_flag&(_IOLBF|_IONBF)) == 0) {
  91.         iop->_cnt--;
  92.         *iop->_ptr++ = c;
  93.     }
  94.     return(c);
  95. }
  96.  
  97. fpurge(iop)
  98. register FILE *iop;
  99. {
  100.     iop->_ptr = iop->_base;
  101.     iop->_cnt = iop->_flag&(_IOLBF|_IONBF|_IOREAD) ? 0 : iop->_bufsiz;
  102.     return(0);
  103. }
  104.  
  105. fflush(iop)
  106. register FILE *iop;
  107. {
  108.     register char *base;
  109.     register n, rn;
  110.  
  111.     if ((iop->_flag&(_IONBF|_IOWRT))==_IOWRT &&
  112.         (base = iop->_base) != NULL && (rn = n = iop->_ptr - base) > 0) {
  113.         iop->_ptr = base;
  114.         iop->_cnt = (iop->_flag&(_IOLBF|_IONBF)) ? 0 : iop->_bufsiz;
  115.         do {
  116.                 n = write(fileno(iop), base, rn);
  117.             if (n <= 0) {
  118.                 iop->_flag |= _IOERR;
  119.                 return(EOF);
  120.             }
  121.             rn -= n;
  122.             base += n;
  123.         } while (rn > 0);
  124.     }
  125.     return(0);
  126. }
  127.  
  128. fclose(iop)
  129.     register FILE *iop;
  130. {
  131.     register int r;
  132.  
  133.     r = EOF;
  134.     if (iop->_flag&(_IOREAD|_IOWRT|_IORW) && (iop->_flag&_IOSTRG)==0) {
  135.         r = fflush(iop);
  136.         if (close(fileno(iop)) < 0)
  137.             r = EOF;
  138.         if (iop->_flag&_IOMYBUF)
  139.             free(iop->_base);
  140.     }
  141.     iop->_cnt = 0;
  142.     iop->_base = (char *)NULL;
  143.     iop->_ptr = (char *)NULL;
  144.     iop->_bufsiz = 0;
  145.     iop->_flag = 0;
  146.     iop->_file = 0;
  147.     return(r);
  148. }
  149.  
  150. _cleanup()
  151. {
  152.     extern int _fwalk();
  153.  
  154.     _fwalk(fclose);
  155. }
  156.